package com.hk.player.utils;

import android.graphics.Bitmap;
import android.graphics.Color;

import com.hk.player.Watermark;
import com.zhoug.common.utils.BitmapUtils;
import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.common.utils.StringUtils;

/**
 * 视频播放水印工具
 *
 * @Author: zhoug
 * @Date: 2023/7/3
 * @Description:
 */
public class WatermarkUtils {
    private static int rowSpacing = -1;//水印行距
    private static int columnSpacing = -1;//水印列距

    /**
     * 给Bitmap上添加水印
     *
     * @return
     */
    public static Bitmap createWatermarkBitmap() {
        //获取水印配置
        Watermark watermark = PlayerDataManager.getInstance().getWatermark();
        if (watermark == null || !watermark.isShowWatermark() || StringUtils.isNullOrEmpty(watermark.getWaterMark())) {
            return null;
        }
        Bitmap bitmap = Bitmap.createBitmap(720, 720 * 16 / 9, Bitmap.Config.ARGB_4444);
        addWatermarkBitmap(bitmap);
        return bitmap;
    }

    /**
     * 图片上添加水印
     * @param bitmap
     */
    public static void addWatermarkBitmap(Bitmap bitmap) {
        //获取水印配置
        Watermark watermark = PlayerDataManager.getInstance().getWatermark();
        if (watermark == null || !watermark.isShowWatermark() || StringUtils.isNullOrEmpty(watermark.getWaterMark())) {
            return;
        }
        new WatermarkBuilder()
                .setContent(watermark.getWaterMark())
                .setTextColor(Color.parseColor("#bebebe"))
                .setAlpha(90)
                .setRowSpacing(getRowSpacing())
                .setColumnSpacing(getColumnSpacing())
                .setBitmap(bitmap)
                .builder();
    }

    /**
     * 图片上添加水印
     * @param imagePath
     */
    public static void addWatermarkBitmap(String imagePath) {
        //获取水印配置
        Watermark watermark = PlayerDataManager.getInstance().getWatermark();
        if (watermark == null || !watermark.isShowWatermark() || StringUtils.isNullOrEmpty(watermark.getWaterMark())) {
            return;
        }
        Bitmap bitmap = BitmapUtils.decodeFile(imagePath, 1080, 1920, Bitmap.Config.ARGB_8888);
        if (bitmap != null) {
            Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            bitmap.recycle();
            bitmap = null;
            addWatermarkBitmap(copy);
            BitmapUtils.keepBitmap(copy, 100, imagePath);
        }

    }

    /**
     * 水印行距
     *
     * @return
     */
    private static int getRowSpacing() {
        if (rowSpacing < 0) {
            rowSpacing = ResourceUtils.dpTopx(20);
        }
        return rowSpacing;
    }

    /**
     * 水印列距
     *
     * @return
     */
    private static int getColumnSpacing() {
        if (columnSpacing < 0) {
            columnSpacing = ResourceUtils.dpTopx(15);
        }
        return columnSpacing;
    }


}
